package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.adapter.CityListAdapter;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.pinyinUtilis.ContactSortModel;
import com.yogee.topseller.utils.pinyinUtilis.PinyinComparator;
import com.yogee.topseller.utils.pinyinUtilis.PinyinUtils;
import com.yogee.topseller.utils.pinyinUtilis.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AllMechanismActivity extends HttpActivity  {


    @BindView(R.id.push_product_rv)
    RecyclerView rv;
    @BindView(R.id.dialog)
    TextView dialog;
    @BindView(R.id.sidrbar)
    SideBar sideBar;
    @BindView(R.id.add_card_titlelayout)
    TitleLayout titlelayout;

    private CityListAdapter adapter;//列表适配器

    private List<ContactSortModel> models = new ArrayList<>();
    private List<String> initModels = new ArrayList<>();

    private List<InterestLabelBean.ListBean.LabelListBean> beans;
    private ArrayList<InterestLabelBean.ListBean.LabelListBean> mSelectBean;

    private InterestLabelBean.ListBean bean;
    private int position = 0;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_allmechanism;
    }

    @Override
    protected void initView() {
        titlelayout.setActivity(this);
        titlelayout.setRightTV("确定");
        position = getIntent().getIntExtra("position",0);
        bean = (InterestLabelBean.ListBean) getIntent().getSerializableExtra("datas");
        if(bean!=null)
            titlelayout.setTitle(bean.getType());
        if(bean.getType().equals("地区")){
            InterestLabelBean interestLabelBean =(InterestLabelBean)getIntent().getSerializableExtra("citys");
            beans = interestLabelBean.getCity();
        }else{
            InterestLabelBean interestLabelBean = (InterestLabelBean) getIntent().getSerializableExtra("citys");
            beans = interestLabelBean.getRelatedCompany();
        }
        mSelectBean = (ArrayList<InterestLabelBean.ListBean.LabelListBean>) getIntent().getSerializableExtra("select");

        adapter = new CityListAdapter(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);

        sideBar.setTextView(dialog);
        initEvents();
        initData();
//        listAdapter.addData(bean);
//        getProductList("0", "10", id, "1");
    }

    private void initData(){
        if(beans!=null&&beans.size()>0){
            models = filledData(beans);
            try{
                Collections.sort(models, new PinyinComparator());
            }catch (Exception e){

            }
            adapter.addData(models,initModels);
        }
    }

    private void initEvents() {
        //设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    rv.scrollToPosition(position + 1);
                }
            }
        });

    }

    private List<ContactSortModel> filledData(List<InterestLabelBean.ListBean.LabelListBean> labelListBeanList) {
        List<ContactSortModel> mSortList = new ArrayList<>();
        ArrayList<String> indexString = new ArrayList<>();

        for (int i = 0; i < labelListBeanList.size(); i++) {
            ContactSortModel sortModel = new ContactSortModel();
            sortModel.setName(labelListBeanList.get(i).getName());
            try{
                sortModel.setId(labelListBeanList.get(i).getId());
            }catch (Exception e){
                sortModel.setId("");
            }

            String pinyin = PinyinUtils.getPingYin(labelListBeanList.get(i).getName());
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")||sortString.matches("[0-9]")) {
                sortModel.setSortLetters(sortString.toUpperCase());
                if (!indexString.contains(sortString)) {
                    indexString.add(sortString);
                }
            }
//            LogUtils.e("data","name:"+labelListBeanList.get(i).getName()+",id:"+labelListBeanList.get(i).getId());
            mSortList.add(sortModel);
        }
        if(mSelectBean!=null&&mSelectBean.size()>0){
            for(int i = 0;i<mSelectBean.size();i++){
                if(!initModels.contains(mSelectBean.get(i).getName())){
                    initModels.add(mSelectBean.get(i).getName());
                }
            }
        }

        Collections.sort(indexString);
        sideBar.setIndexText(indexString);
        return mSortList;
    }
    @OnClick({R.id.layout_title_right_tv})
    public void onViewClicked(View view){
        switch (view.getId()){

            case R.id.layout_title_right_tv:
//                finish();
//                Intent intent = new Intent();
//                setResult(0,intent);
                List<InterestLabelBean.ListBean.LabelListBean> list = adapter.getSelectedItem();
                if(list!=null&&list.size()>0){
                    bean.getLabelList().clear();
                    bean.setLabelList(list);
//                ToastUtils.showToast(AllMechanismActivity.this,"选择的条数:"+list.size());
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("selectlist",bean);
                    intent.putExtras(bundle);
                    intent.putExtra("position",position);
                    setResult(1,intent);
                    finish();
                }else{
                    ToastUtils.showToast(AllMechanismActivity.this,"至少选中一条数据");
                }


                break;
        }


    }

}